package com.gitee.dprecated.state;

import com.gitee.dprecated.observer.AbstractOrderObserver;
import com.gitee.dprecated.observer.OrderStateEnum;
import com.gitee.utils.RedisCommonProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 商品订单状态转化与处理 抽象被观察者类
 * 观察者类：观察者类有2个工作：
 * 1、添加和删除观察者
 * 2、通知消息到观察者类
 *
 * @author acao
 * @since 2023-11-07 09:56
 */
@Component
public class CreateOrder extends AbstractOrder {

    //引入redis，存储订单
    @Autowired
    private RedisCommonProcessor redisCommonProcessor;

    @Override
    void addOrderObserver(AbstractOrderObserver orderObserver) {

    }

    @Override
    void removeOrderObserver(AbstractOrderObserver orderObserver) {

    }

    public OrderPO createOrder(String orderId, String productId) {
        //创建订单对象，设置状态为 ORDER_WAIT_PAY
        OrderPO order = OrderPO.builder()
                .orderId(orderId)
                .productId(productId)
                .state(OrderStateEnum.ORDER_WAIT_PAY)
                .build();
        //将新订单存入 redis缓存，15分钟后失效
        redisCommonProcessor.set(orderId, order, 900);
        //观察者模式：发送订单创建Event（4.5小节和4.6小节进行实现）
        super.notifyObserver(orderId, OrderStateEnum.ORDER_WAIT_PAY);
        return order;
    }

}
